package com.github.wui.NoHeardActionBar;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;

import com.github.wui.R;

/**
 * 自动适应状态栏
 *
 * @author Mr.xie
 * @Date 2025/4/24
 */
public class NoHeardActionBar extends RelativeLayout {

    private float topOffset = 0;

    public NoHeardActionBar(Context context) {
        super(context);
        init(context, null);
    }

    public NoHeardActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public NoHeardActionBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        @SuppressLint("InternalInsetResource") int identifier = getResources().getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = getResources().getDimensionPixelOffset(identifier);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        int finalSpec = MeasureSpec.makeMeasureSpec(parentHeight + statusBarHeight, MeasureSpec.EXACTLY);
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            child.setPadding(0, (int) (statusBarHeight / 2 + topOffset), 0, 0);
        }
        super.onMeasure(widthMeasureSpec, finalSpec);
    }


    void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NoHeardActionBar);
        topOffset = typedArray.getDimension(R.styleable.NoHeardActionBar_topOffset, 0);
        typedArray.recycle();
    }


}
